﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using HousingInfo.Library;

namespace Admin.Admin
{
    public partial class Users : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            //if ((Session["LOGIN"] == null) || (int)Session["ROLES"] != 1)
            //{
            //    Response.Redirect("Login.aspx");
            //}
            if (!IsPostBack)
            {
                listRoles.DataSource = RoleInfo.GetRoleInfo();
                listRoles.DataBind();
                dropdownlistQuestion.DataSource = QuestionInfo.GetQuestionInfo();
                dropdownlistQuestion.DataBind();
            }

            string mode = Request.QueryString["Mode"];
            //Mode Add
            if (mode == "1")
            {
                btnUpdate.Visible = false;
                btnDelete.Visible = false;
                txtusername.ReadOnly = false;
                LabelRegisterDate.Visible = false;
                txtregisterdate.Visible = false;
                listRoles.SelectedIndex = 1;
                PanelActions.Visible = true;
                PanelDasboard.Visible = false;
                lbHeaderActions.Text = "Thêm thành viên";
                GridView1.Columns[0].Visible = false;
            }
            else //Mode Update
            {
                btnAdd.Visible = false;
                txtusername.Enabled = false;
                txtpassword.Enabled = false;
                lbHeaderActions.Text = "Sửa thành viên";
            }

        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string username = GridView1.SelectedRow.Cells[1].Text.ToString();
            UserBO userbo = UserBO.GetUserBO(username);
            txtusername.Text = userbo.UserName;
            txtpassword.Text = userbo.Password;
            //listRoles.SelectedIndex = userbo.RoleID.Value - 1;
            listRoles.SelectedValue = userbo.RoleID.ToString();
            string vl = listRoles.SelectedValue;
            txtfullname.Text = userbo.FullName;
            txtbirth.Text = userbo.BirthDay.ToString();
            txtemail.Text = userbo.Email;
            txtnickyahoo.Text = userbo.NickYahoo;
            txtnickskyer.Text = userbo.NickSkyper;
            txttelephone.Text = userbo.TelePhone;
            txtmobiphone.Text = userbo.MobilePhone;
            txtaddress.Text = userbo.Address;
            cbbProvincy.SelectedValue = userbo.City;
            txturlwebsite.Text = userbo.UrlWebsite;
            txtregisterdate.Text = userbo.RegisterDate.Value.ToString("dd/MM/yyyy");
            dropdownlistQuestion.SelectedValue = userbo.QuestionID.ToString();
            //dropdownlistQuestion.SelectedValue = userbo.QuestionID.ToString();
            txtAnswerQuestion.Text = userbo.AnswerQuestion;
            isLock.Checked = userbo.Islock;
            txtlockdate.Text = userbo.RegisterDate.Value.ToString("dd/MM/yyyy");
            txtunlockdate.Text = userbo.RegisterDate.Value.ToString("dd/MM/yyyy");
            isActive.Checked = userbo.IsActive;
            message.Text = "";
            if (PanelActions.Visible == false)
                PanelActions.Visible = true;
            messagedelete.Text = "";

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {

            message.Text = "";
            UserBO userbo = UserBO.GetUserBO(txtusername.Text);
            userbo.FullName = txtfullname.Text;
          //  userbo.RoleID = listRoles.SelectedIndex + 1;
            try
            {
                userbo.BirthDay = Convert.ToDateTime(txtbirth.Text);
            }
            catch
            {
                var convert = txtbirth.Text.Split('/');
                userbo.BirthDay = Convert.ToDateTime(convert[1].ToString() + "/" + convert[0].ToString() + "/" + convert[2].ToString());
            }
            userbo.Email = txtemail.Text;
            userbo.NickYahoo = txtnickyahoo.Text;
            userbo.NickSkyper = txtnickskyer.Text;
            userbo.TelePhone = txttelephone.Text;
            userbo.MobilePhone = txtmobiphone.Text;
            userbo.Address = txtaddress.Text;
           // userbo.City = cbbProvincy.SelectedValue.ToString();
            userbo.UrlWebsite = txturlwebsite.Text;
           // userbo.QuestionID = int.Parse(dropdownlistQuestion.SelectedValue);
            userbo.AnswerQuestion = txtAnswerQuestion.Text;
            userbo.Islock = isLock.Checked;
            userbo.IsActive = isActive.Checked;
            try
            {
                userbo.Save();
                message.Text = "Cập nhật thành công !";
            }
            catch
            {
                message.Text = "Cập nhật thất bại !";
            }

            GridView1.DataBind();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {

            if (NewsHouseBOList.CheckNewsHouseinAreaDetail(null, txtusername.Text, null))
            {

                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success", "alert('Bạn phải xóa những tin do người dùng này đăng trước !!! !!!')", true);

                return;
            }

            try
            {
                UserBO.DeleteUserBO(txtusername.Text);
                messagedelete.Text = "Xóa thành công !";
                PanelActions.Visible = false;
            }
            catch
            {
                message.Text = "Xóa thất bại !";
            }
            GridView1.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
             if (!UserBO.CheckUserNameExist(txtusername.Text))
            {
                UserBO userbo = UserBO.NewUserBO(txtusername.Text);
                userbo.FullName = txtfullname.Text;
                //userbo.BirthDay = txtbirth.Text;
                DateTime dateti;
                dateti = Convert.ToDateTime(txtbirth.Text);
                try
                {
                    userbo.BirthDay = Convert.ToDateTime(dateti.ToString("dd/MM/yyyy"));
                }
                catch
                {
                    userbo.BirthDay = Convert.ToDateTime(dateti.ToString("MM/dd/yyyy"));
                }
                userbo.Email = txtemail.Text;
                userbo.Password = UserBO.encryptData(txtpassword.Text);
                userbo.RoleID = listRoles.SelectedIndex + 1;
                userbo.NickYahoo = txtnickyahoo.Text;
                userbo.NickSkyper = txtnickskyer.Text;
                userbo.TelePhone = txttelephone.Text;
                userbo.MobilePhone = txtmobiphone.Text;
                userbo.Address = txtaddress.Text;
                userbo.City = cbbProvincy.SelectedValue.ToString();
                userbo.UrlWebsite = txturlwebsite.Text;
                userbo.IsActive = isActive.Checked;
                userbo.Islock = isLock.Checked;
                userbo.RegisterDate = DateTime.Now;
                userbo.LockDate = DateTime.Now;
                userbo.UnlockDate = DateTime.Now;
                userbo.QuestionID = int.Parse(dropdownlistQuestion.SelectedValue);
                userbo.AnswerQuestion = txtAnswerQuestion.Text;

                try
                {
                    userbo.Save();
                    message.Text = "Thêm thành công !";
                    resetControls();
                }
                catch
                {
                    message.Text = "Thêm thất bại !";
                }
            }

            else
            {
                message.Text = "Tài khoản đã tồn tại !";
            }
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            
        }

        protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {
           //// Response.Write("<script>alert('Hello')</script>");
        }

        private void resetControls()
        {
            //txtusername.Text = "";
            //txtpassword.Text = "";
            //txtfullname.Text = "";
            //txtnickyahoo.Text = "";
            //txttelephone.Text = "";
            //txtmobiphone.Text = "";
            //txturlwebsite.Text = "http://";
            //txtaddress.Text = "";
            //txtAnswerQuestion.Text = "";
            //dropdownlistQuestion.DataSource = QuestionInfo.GetQuestionInfo();
        }
    }
}
